//
//  ReportExceptionViewController.m
//  YuTang
//
//  Created by 葛悦彣 on 2024/8/19.
//

#import "ReportExceptionViewController.h"
#import "YTBaseSelectStringView.h"
#import "DTChoosePersonListShowView.h"
#import "AddPondModel.h"
#import "foundQuestSelectPondShowView.h"
#import "YTSelectStringShowView.h"
#import "VoiceView.h"

@interface ReportExceptionViewController ()<UITextViewDelegate>
@property (nonatomic ,strong) YTBaseSelectStringView * bzView;
@property (nonatomic ,strong) YTBaseSelectStringView * mcView;
///池塘编组
@property (nonatomic ,strong) NSString * pondId;
///名称
@property (nonatomic ,strong) NSString * pondName;
@property (nonatomic ,strong) NSString * tissue;
@property (nonatomic ,strong) NSString * tissueId;

@property (nonatomic ,strong) IQTextView * dataview;
@property (nonatomic ,strong) UILabel * StrLab;
@property (nonatomic ,strong) VoiceView * voice;
@end

@implementation ReportExceptionViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.view.backgroundColor = KcolorHex(@"#EBEDF0");

    UIView * topview = [UIView new];
    topview.backgroundColor = KcolorHex(@"#0967FF");
    [self.view addSubview:topview];
    [topview mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.top.right.offset(0);
        make.height.offset(Knav_HeightX);
    }];
    MJWeakSelf
    UIView * backview = [UIView new].addClickBlock(^{
        [weakSelf popViewController];
    });
    [topview addSubview:backview];
    [backview mas_makeConstraints:^(MASConstraintMaker *make) {
        make.height.offset(44);
        make.left.bottom.offset(0);
        make.width.offset(kwt(120));
    }];
    
    UIImageView * leftimg = kimageView(@"white_path");
    [backview addSubview:leftimg];
    [leftimg mas_makeConstraints:^(MASConstraintMaker *make) {
        make.width.height.offset(20);
        make.left.offset(kwt(10));
        make.centerY.offset(0);
    }];
        
    UILabel * leftlab = [DTKTool labelWithText:@"上报异常" andFont:kCFont(18) andTextColor:UIColor.whiteColor];
    [backview addSubview:leftlab];
    [leftlab mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.offset(kwt(43));
        make.centerY.offset(0);
    }];
    
    UIView * buttonview = [UIView new];
    buttonview.backgroundColor = UIColor.whiteColor;
    [self.view addSubview:buttonview];
    [buttonview mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.right.bottom.offset(0);
        make.height.offset(kwt(63) + KSafeAreaBottomHeight);
    }];
    
    UIButton * qxbtn = [UIButton buttonWithType:UIButtonTypeCustom];
    qxbtn.backgroundColor = KcolorHex(@"#F0F2F4");
    [qxbtn setTitle:@"取消" forState:UIControlStateNormal];
    [qxbtn setTitleColor:KcolorHex(@"#5A6066") forState:UIControlStateNormal];
    qxbtn.titleLabel.font = kCFont(17);
    [DTKTool YJ:kwt(5) andView:qxbtn];
    [qxbtn addTarget:self action:@selector(leftBtnClick) forControlEvents:UIControlEventTouchUpInside];
    [buttonview addSubview:qxbtn];
    [qxbtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.height.offset(kwt(48));
        make.width.offset(kwt(133));
        make.left.offset(kwt(15));
        make.top.offset(kwt(8));
    }];
    
    UIButton * qrbtn = [UIButton buttonWithType:UIButtonTypeCustom];
    qrbtn.backgroundColor = [UIColor gradientColorWithSize:CGSizeMake(kwt(195), 1) direction:GradientColorDirectionLevel startColor:KcolorHex(@"#2892FE") endColor:KcolorHex(@"#0366FE")];
    [qrbtn setTitle:@"提交" forState:UIControlStateNormal];
    [qrbtn setTitleColor:UIColor.whiteColor forState:UIControlStateNormal];
    qrbtn.titleLabel.font = kCFont(17);
    [DTKTool YJ:kwt(5) andView:qrbtn];
    [qrbtn addTarget:self action:@selector(rightBtnClick) forControlEvents:UIControlEventTouchUpInside];
    [buttonview addSubview:qrbtn];
    [qrbtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.height.offset(kwt(48));
        make.width.offset(kwt(195));
        make.right.offset(kwt(-15));
        make.top.offset(kwt(8));
    }];

    
    YTBaseSelectStringView * bzView = [YTBaseSelectStringView NewViewWithNil];
    bzView.backgroundColor = UIColor.whiteColor;
    bzView.name = @"池塘编组";
    bzView.forceFlag = YES;
    bzView.block = ^(NSString * _Nonnull content) {
        [weakSelf bzClick];
    };
    [DTKTool YJ:kwt(5) andView:bzView];
    self.bzView = bzView;
    [self.view addSubview:bzView];
    [bzView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.offset(kwt(10));
        make.right.offset(kwt(-10));
        make.height.offset(kwt(53));
        make.top.equalTo(topview.mas_bottom).offset(kwt(12));
    }];
    
    YTBaseSelectStringView * mcView = [YTBaseSelectStringView NewViewWithNil];
    mcView.name = @"池塘名称";
    mcView.backgroundColor = UIColor.whiteColor;
    mcView.forceFlag = YES;
    mcView.block = ^(NSString * _Nonnull content) {
        [weakSelf mcClick];
    };
    [DTKTool YJ:kwt(5) andView:mcView];
    self.mcView = mcView;
    [self.view addSubview:mcView];
    [mcView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.offset(kwt(10));
        make.right.offset(kwt(-10));
        make.height.offset(kwt(53));
        make.top.equalTo(bzView.mas_bottom).offset(kwt(10));
    }];
    UIView * makeview = [UIView new];
    makeview.backgroundColor = KcolorHex(@"#F6F7F9");
    [DTKTool YJ:kwt(5) andView:makeview];
    [self.view addSubview:makeview];
    [makeview mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.offset(kwt(10));
        make.right.offset(kwt(-10));
        make.top.equalTo(mcView.mas_bottom).offset(kwt(10));
        make.height.offset(kwt(155));
    }];
    
    UILabel * name = [DTKTool labelWithText:@"异常说明" andFont:kCFont(18) andTextColor:KcolorHex(@"#222222")];
    [makeview addSubview:name];
    [name mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.offset(kwt(10));
        make.top.offset(kwt(18));
    }];
    
    
    UILabel * StrLab = [DTKTool labelWithText:@"0/150" andFont:kCFont(17) andTextColor:KcolorHex(@"#787A79")];
    [makeview addSubview:StrLab];
    self.StrLab = StrLab;
    [StrLab mas_makeConstraints:^(MASConstraintMaker *make) {
        make.right.offset(kwt(-12));
        make.centerY.equalTo(name);
    }];
    
    
    
    IQTextView * textview = [IQTextView new];
    textview.textContainer.lineFragmentPadding = 0;
    textview.textColor = KcolorHex(@"#222222");
    textview.font = kFont(17);
    self.dataview = textview;
    textview.placeholder = @"请填写异常说明";
    textview.delegate = self;
    textview.backgroundColor = UIColor.clearColor;
    [makeview addSubview:textview];
    [textview mas_makeConstraints:^(MASConstraintMaker *make) {
         make.left.offset(kwt(10));
         make.right.offset(kwt(-10));
         make.top.offset(kwt(50));
         make.bottom.offset(kwt(-10));
    }];

    VoiceView * voice = [VoiceView NewView];
    [DTKTool YJ:kwt(5) andView:voice];
    voice.SoundAndTimeBlock = ^(NSString * _Nonnull Url, NSString * _Nonnull previewUrl, NSString * _Nonnull voiceDuration) {
        weakSelf.voice.voiceUrl = previewUrl;
        weakSelf.voice.TJvoiceUrl = Url;
        weakSelf.voice.voiceDuration = voiceDuration;
    };
    voice.removeBlock = ^{
        weakSelf.voice.voiceUrl = @"";
        weakSelf.voice.TJvoiceUrl = @"";
        weakSelf.voice.voiceDuration = @"";
    };
    self.voice  = voice;
    [self.view addSubview:voice];
    [voice mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.offset(kwt(12));
        make.right.offset(kwt(-12));
        make.top.equalTo(makeview.mas_bottom).offset(kwt(10));
        make.height.offset(kwt(53));

    }];
}

-(void)textViewDidChange:(UITextView *)textView{
    if(textView.text.length > 150){
        textView.text = [textView.text substringToIndex:150];
    }
    self.StrLab.text = [NSString stringWithFormat:@"%lu/150",(unsigned long)textView.text.length];
}
- (void)bzClick{
    [self.view showBusyHUD];
    [SCNetManger PostPondTissueGetPondTissueListWithDict: [NSMutableDictionary dictionary] andCompetionHandlerBlock:^(HTTPArrBase * model) {
        [self.view hideBusyHUD];
        if(model.code ==200){
            NSArray <getPondTissueListModel *>* array = [NSArray yy_modelArrayWithClass:getPondTissueListModel.class json:model.result ];
            
            getPondTissueListModel * mm = [getPondTissueListModel new];
            mm.children = array;
            if(self.tissueId.length > 0){
                [mm modelWithSeledID:self.tissueId];
            }
            NSLog(@"%@",array);
            MJWeakSelf
            [DTChoosePersonListShowView showViewWithArray:array andBlock:^(getPondTissueListModel * _Nonnull pondModel) {
                weakSelf.tissueId = pondModel.ID;
                weakSelf.tissue = pondModel.TJname;
                weakSelf.bzView.content = pondModel.TJname;
                weakSelf.pondName = @"";
                weakSelf.pondId = @"";
                weakSelf.mcView.content = @"";
            }];
        } else {
            [self.view showWarning:model.message];
        }
    }];
}
- (void)mcClick{
    if(self.tissueId.length < 1){
        [self.view showWarning:@"请先选择池塘编组"];
        return;
    }
    [self.view showBusyHUD];
    NSMutableDictionary * dict = [NSMutableDictionary dictionary];
    [dict setObject:self.tissueId forKey:@"tissueId"];
    MJWeakSelf
    [SCNetManger PostTaskPondListWithDict:dict  andCompetionHandlerBlock:^(HTTPArrBase * model) {
        [self.view hideBusyHUD];
        if(model.code ==200){
            NSArray <pondModelListModel *>* Modelarray = [NSArray yy_modelArrayWithClass:pondModelListModel.class json:model.result ];
            NSMutableArray * nameArray = [NSMutableArray array];
            ///数据清理
            int index = 0;
            int i = 0;
            for (pondModelListModel * tempmodel in Modelarray) {
                if([self.pondName isEqualToString:tempmodel.ID]){
                    index = i;
                }
                i++;
                [nameArray addObject:tempmodel.name];
            }
            [YTSearchStringShowView showViewWithTitle:@"池塘名称" andDateArray:nameArray andBlock:^(NSString * _Nonnull str) {
                for (pondModelListModel * tempmodel in Modelarray) {
                    if([str isEqualToString:tempmodel.name]){
                        weakSelf.pondName = tempmodel.name;
                        weakSelf.pondId = tempmodel.ID;
                        weakSelf.mcView.content = str;
                    }
                }
            }];
        } else {
            [self.view showWarning:model.message];
        }
    }];
    
}

- (void)leftBtnClick{
    [self.navigationController popViewControllerAnimated:YES];
}

- (void)rightBtnClick{
    if(self.tissueId.length < 1){
        [self.view showWarning:@"请先选择池塘编组"];
        return;
    }
    if(self.pondId.length < 1){
        [self.view showWarning:@"请先选择池塘名称"];
        return;
    }
    NSMutableDictionary * dict = [NSMutableDictionary dictionary];
    [dict setObject:self.pondId forKey:@"pondId"];
    [dict setObject:self.pondName forKey:@"pondName"];
    [dict setObject:self.tissue forKey:@"tissue"];
    [dict setObject:self.tissueId forKey:@"tissueId"];
    [dict setObject:self.dataview.text forKey:@"data"];
    [dict setObject:self.voice.TJvoiceUrl forKey:@"remark"];
    [self.view showBusyHUD];
    [SCNetManger  PostCardSubmitWithDict:dict andCompetionHandlerBlock:^(HTTPDicBase * model) {
        if(model.code == 200){
            [self.view showWarning:model.message];
            dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(2 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
                // 2秒后执行的代码
                [self.navigationController popViewControllerAnimated:YES];
            });
        } else {
            [self.view showWarning:model.message];
        }
    }];
    
}


@end
